<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        title: {
          text: '隐患类型',
          textStyle: {
            color: '#ffffff'
          },
          x: 'left'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: [
          {
            x: '55px',
            y: '260px',
            data: ['机械安全', '温度保护', '环境保护', '化学品安全'],
            inactiveColor: '#5e5e5e',
            textStyle: {
              color: '#ffffff'
            }
          },
          {
            x: '55px',
            y: '280px',
            data: ['交通安全', '坠落安全', '电气安全', '其他隐患'],
            inactiveColor: '#5e5e5e',
            textStyle: {
              color: '#ffffff'
            }
          }
        ],
        series: [
          {
            name: 'WEEKLY WRITE ARTICLES',
            type: 'pie',
            roseType: 'radius',
            radius: [15, 95],
            center: ['50%', '48%'],
            data: [
              { value: 320, name: '机械安全' },
              { value: 240, name: '温度保护' },
              { value: 149, name: '环境保护' },
              { value: 100, name: '化学品安全' },
              { value: 59, name: '交通安全' },
              { value: 42, name: '坠落安全' },
              { value: 130, name: '电气安全' },
              { value: 10, name: '其他隐患' }
            ],
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>
